//
//  SEGIntegralExchangeConfirmCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/6/1.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGIntegralExchangeConfirmCell.h"

#if __has_include(<SEGPlatform/CommonViewsHeader.h>)
#import <SEGPlatform/CommonViewsHeader.h>
#else
#import "CommonViewsHeader.h"
#endif

#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGBussinessConstHeader.h>)
#import <Commom/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

@interface SEGIntegralExchangeConfirmCell ()<UITextFieldDelegate>

/**
 底部描述标签
 */
@property (nonatomic, strong)UILabel *descLabel;

/**
 底部减号按钮
 */
@property (nonatomic, strong)UIButton *minusButton;

/**
 底部加号按钮
 */
@property (nonatomic, strong)UIButton *plusButton;
@end


@implementation SEGIntegralExchangeConfirmCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = kColor_W;
    [self setUpSubviews];
    
    return self;
}

- (void)setUpSubviews
{
    // 图片
    self.picImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.picImageView];
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).mas_offset(15);
        make.top.equalTo(self.contentView.mas_top).mas_offset(15);
        make.width.mas_equalTo(69);
        make.height.mas_equalTo(69);
    }];
    
    // 名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = kColor_1;
    self.nameLabel.font = kFont_16;
    self.nameLabel.numberOfLines = 2;
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.picImageView.mas_right).with.offset(15);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.top.equalTo(self.contentView.mas_top).with.offset(16);
    }];
    
    // 积分数量
    self.iconsLabel = [[UILabel alloc] init];
    self.iconsLabel.textColor = kColor_3;
    self.iconsLabel.font = kFont_13;
    [self.contentView addSubview:self.iconsLabel];
    [self.iconsLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.picImageView.mas_right).with.offset(15);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(10);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.equalTo(self.picImageView.mas_bottom).mas_offset(15);
        make.height.mas_equalTo(0.5);
    }];
    
    //兑换数量描述标签
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kColor_1;
    label.font = kFont_13;
    [self.contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView).with.offset(15);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(self.contentView);
    }];
    self.descLabel = label;
}

- (void)showWithModel:(SEGIntegralExchangeConfirmModel*)model count:(NSUInteger)count
{
    NSString* iconString = [NSString stringWithFormat:@"%@/%@", kClientImageRootURL, model.imgUrl];
    [self.picImageView setImageWithURL:[NSURL URLWithString:iconString] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_70_70")]];
    
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.name];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.name length])];
    self.nameLabel.attributedText = attributedString;
    
    self.iconsLabel.text = [NSString stringWithFormat:@"%@ 积分",model.coins];
    SEGIntegralProductType type = [model.type integerValue];
    switch (type) {
        case SEGIntegralProductType_OnlineLogistics:
        case SEGIntegralProductType_Property:
        {
            self.descLabel.text = @"兑换数量";
            [self seg_setCountHidden:NO];
            self.phoneField.hidden = YES;
            self.accountField.hidden = YES;
        }
            break;
        case SEGIntegralProductType_OnlineExchangeCode:
        {
            self.descLabel.text = @"联系电话";
            [self seg_setCountHidden:YES];
            self.phoneField.hidden = NO;
            self.accountField.hidden = YES;
        }
            break;
        case SEGIntegralProductType_OnlineVirtual:
        {
            self.descLabel.text = @"充值手机号码或账号";
            [self seg_setCountHidden:YES];
            self.phoneField.hidden = YES;
            self.accountField.hidden = NO;
        }
            break;
    }
    self.countLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)count];
}

//设置是否显示数量
- (void)seg_setCountHidden:(BOOL)hidden
{
    self.minusButton.hidden = hidden;
    self.plusButton.hidden = hidden;
    self.countLabel.hidden = hidden;
}

- (void)seg_minus
{
    NSUInteger count = [self.countLabel.text integerValue];
    if (count > 1) {
        count--;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeToCount:shouldRequest:)]) {
        [self.delegate changeToCount:count shouldRequest:NO];
    }
}

- (void)seg_plus
{
    NSUInteger count = [self.countLabel.text integerValue];
    count++;
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeToCount:shouldRequest:)]) {
        [self.delegate changeToCount:count shouldRequest:YES];
    }
}

//限制phoneField输入长度 最大输入长度为11个字符
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    if (textField == self.phoneField) {
        if (string.length == 0)
            return YES;
        
        //不能输入数字以外的字符
        NSString *temp = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
        if(temp.length > 0)
        {
            return NO;
        }
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 11) {
            return NO;
        }
    } else if (textField == self.accountField) {
        if (string.length == 0)
            return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 50) {
            [SEGAlertView showTmepMessageWithMessage:@"充值号码或账号最多50个字符"];
            return NO;
        }
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [self endEditing:YES];
    return YES;
}

#pragma mark - getter
- (UITextField *)phoneField
{
    if (!_phoneField) {
        _phoneField = [[UITextField alloc] init];
        _phoneField.font = kFont_13;
        _phoneField.textColor = kColor_1;
        [_phoneField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入手机号码" attributes:@{NSForegroundColorAttributeName:kColor_3}]];
        [self.contentView addSubview:_phoneField];
        [_phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).mas_offset(99);
            make.centerY.equalTo(self.descLabel);
            make.right.equalTo(self.contentView).mas_offset(-15);
        }];
        _phoneField.delegate = self;
        _phoneField.returnKeyType = UIReturnKeyDone;
    }
    return _phoneField;
}

- (UITextField *)accountField
{
    if (!_accountField) {
        _accountField = [[UITextField alloc] init];
        _accountField.font = kFont_13;
        _accountField.textColor = kColor_1;
        [_accountField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入需要充值的号码" attributes:@{NSForegroundColorAttributeName:kColor_3}]];
        [self.contentView addSubview:_accountField];
        [_accountField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.descLabel.mas_right).mas_offset(20);
            make.centerY.equalTo(self.descLabel);
            make.right.equalTo(self.contentView).mas_offset(-15);
        }];
        _accountField.delegate = self;
        _accountField.returnKeyType = UIReturnKeyDone;
    }
    return _accountField;
}

- (UIButton *)minusButton
{
    if (!_minusButton) {
        _minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusButton addTarget:self action:@selector(seg_minus) forControlEvents:UIControlEventTouchUpInside];
        [_minusButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_cut")] forState:UIControlStateNormal];
        [self.contentView addSubview:_minusButton];
        [_minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.descLabel);
            make.right.equalTo(self.countLabel.mas_left);
        }];
    }
    return _minusButton;
}

- (UIButton *)plusButton
{
    if (!_plusButton) {
        _plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_plusButton addTarget:self action:@selector(seg_plus) forControlEvents:UIControlEventTouchUpInside];
        [_plusButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_add")] forState:UIControlStateNormal];
        [self.contentView addSubview:_plusButton];
        [_plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.descLabel);
            make.right.equalTo(self.contentView).mas_offset(-15);
        }];
    }
    return _plusButton;
}

- (UILabel *)countLabel
{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.textColor = kColor_1;
        _countLabel.font = kFont_13;
        _countLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_countLabel];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerY.equalTo(self.descLabel);
            make.right.equalTo(self.plusButton.mas_left);
            make.width.mas_equalTo(46);
        }];
        _countLabel.text = @"1";
    }
    return _countLabel;
}
@end
